# -*- coding: utf-8 -*-
"""
测试用例生成工具GUI界面
"""
import tkinter as tk
from tkinter import filedialog, messagebox
import os
from Create_testcase import Testcase

class TestcaseGUI:
    """
    测试用例生成工具的图形界面类
    """
    def __init__(self, master):
        """
        初始化GUI界面
        :param master: Tkinter根窗口
        """
        self.master = master
        master.title("测试用例生成工具")
        master.geometry("600x500")
        
        # 初始化上次选择的路径
        self.last_path = os.getcwd() + "\\allure-results"
        self.last_output_dir = os.getcwd()  # 新增：记忆上次输出目录
        
        # 创建控件
        self.create_widgets()
        
    def create_widgets(self):
        """
        创建界面控件
        """
        # 结果文件路径
        tk.Label(self.master, text="结果文件路径:").grid(row=0, column=0, sticky="w", padx=10, pady=5)
        self.path_entry = tk.Entry(self.master, width=50)
        self.path_entry.grid(row=0, column=1, padx=5, pady=5)
        self.path_entry.insert(0, os.getcwd() + "\\allure-results")
        
        tk.Button(self.master, text="浏览", command=self.browse_path).grid(row=0, column=2, padx=5, pady=5)
        
        # 输出文件名
        tk.Label(self.master, text="输出文件名:").grid(row=1, column=0, sticky="w", padx=10, pady=5)
        self.output_entry = tk.Entry(self.master, width=50)
        self.output_entry.grid(row=1, column=1, padx=5, pady=5)
        
        # 参数设置
        self.create_parameter_widgets()
        
        # 生成按钮
        tk.Button(self.master, text="生成测试用例与报告", command=self.generate_report, 
                 bg="#4CAF50", fg="white").grid(row=12, column=1, pady=20)
        
    def create_parameter_widgets(self):
        """
        创建参数设置控件
        """
        # 功能模块
        tk.Label(self.master, text="功能模块:").grid(row=2, column=0, sticky="w", padx=10, pady=5)
        self.module_entry = tk.Entry(self.master, width=50)
        self.module_entry.grid(row=2, column=1, padx=5, pady=5)
        
        # 编号
        tk.Label(self.master, text="编号:").grid(row=3, column=0, sticky="w", padx=10, pady=5)
        self.numb_entry = tk.Entry(self.master, width=50)
        self.numb_entry.grid(row=3, column=1, padx=5, pady=5)
        
        # 维护人
        tk.Label(self.master, text="维护人:").grid(row=4, column=0, sticky="w", padx=10, pady=5)
        self.maintainers_entry = tk.Entry(self.master, width=50)
        self.maintainers_entry.grid(row=4, column=1, padx=5, pady=5)
        self.maintainers_entry.insert(0, "杨亮")
        
        # 用例类型
        tk.Label(self.master, text="用例类型:").grid(row=5, column=0, sticky="w", padx=10, pady=5)
        self.type_entry = tk.Entry(self.master, width=50)
        self.type_entry.grid(row=5, column=1, padx=5, pady=5)
        self.type_entry.insert(0, "功能测试")
        
        # 重要程度
        tk.Label(self.master, text="重要程度:").grid(row=6, column=0, sticky="w", padx=10, pady=5)
        self.level_entry = tk.Entry(self.master, width=50)
        self.level_entry.grid(row=6, column=1, padx=5, pady=5)
        self.level_entry.insert(0, "High")
        
        # 测试用例设计方法
        tk.Label(self.master, text="测试用例设计方法:").grid(row=7, column=0, sticky="w", padx=10, pady=5)
        self.link_entry = tk.Entry(self.master, width=50)
        self.link_entry.grid(row=7, column=1, padx=5, pady=5)
        self.link_entry.insert(0, "基于需求的设计方法")
        
        # 测试方法类型
        tk.Label(self.master, text="测试方法类型:").grid(row=8, column=0, sticky="w", padx=10, pady=5)
        self.method_entry = tk.Entry(self.master, width=50)
        self.method_entry.grid(row=8, column=1, padx=5, pady=5)
        self.method_entry.insert(0, "诊断测试")
        
        # 测试环境
        tk.Label(self.master, text="测试环境:").grid(row=9, column=0, sticky="w", padx=10, pady=5)
        self.env_entry = tk.Entry(self.master, width=50)
        self.env_entry.grid(row=9, column=1, padx=5, pady=5)
        self.env_entry.insert(0, "自动化测试平台")
        
        # 前置条件
        tk.Label(self.master, text="前置条件:").grid(row=10, column=0, sticky="w", padx=10, pady=5)
        self.precondition_entry = tk.Entry(self.master, width=50)
        self.precondition_entry.grid(row=10, column=1, padx=5, pady=5)
        
        # 关注人
        tk.Label(self.master, text="关注人:").grid(row=11, column=0, sticky="w", padx=10, pady=5)
        self.focus_entry = tk.Entry(self.master, width=50)
        self.focus_entry.grid(row=11, column=1, padx=5, pady=5)
        
    def browse_path(self):
        """
        浏览文件夹
        """
        path = filedialog.askdirectory(initialdir=self.last_path)
        if path:
            self.path_entry.delete(0, tk.END)
            self.path_entry.insert(0, path)
            self.last_path = path
    
    def generate_report(self):
        """
        生成测试报告
        """
        try:
            path = self.path_entry.get()
            if not os.path.exists(path):
                messagebox.showerror("错误", f"路径不存在: {path}")
                return
                
            # 获取所有参数
            output = self.output_entry.get()
            module = self.module_entry.get()
            numb = self.numb_entry.get()
            maintainers = self.maintainers_entry.get()
            type_ = self.type_entry.get()
            level = self.level_entry.get()
            link = self.link_entry.get()
            method = self.method_entry.get()
            env = self.env_entry.get()
            precondition = self.precondition_entry.get()
            focus = self.focus_entry.get()
            
            # 创建测试用例实例
            tc = Testcase(path)
            if output:
                tc.output_file_name = output
                
            # 获取结果文件并生成报告
            tc.get_result_file()
            tc.get_testcase()
            tc.get_data(module, numb, maintainers, type_, level, link, 
                       method, env, precondition, focus)
            
            # 选择输出目录
            output_dir = filedialog.askdirectory(
                title="选择测试报告保存位置", 
                initialdir=self.last_output_dir
            )
            if not output_dir:
                return
                
            # 检查文件是否已存在
            output_file = os.path.join(output_dir, f"{tc.output_file_name if hasattr(tc, 'output_file_name') else 'TestReport'}.xlsx")
            if os.path.exists(output_file):
                if not messagebox.askyesno("确认", f"文件 {os.path.basename(output_file)} 已存在，是否覆盖？"):
                    return
                
            # 写入Excel
            tc.write_excel(output_dir)
            self.last_output_dir = output_dir  # 保存本次选择的目录
            
            messagebox.showinfo("成功", f"测试报告已生成到: {output_dir}")
            
        except Exception as e:
            messagebox.showerror("错误", f"生成报告失败: {str(e)}")

if __name__ == "__main__":
    root = tk.Tk()
    app = TestcaseGUI(root)
    root.mainloop()